package com.lvmama.rhino.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.lvmama.rhino.pojo.ActiveUser;
import com.lvmama.rhino.service.ActiveUserService;
import com.lvmama.rhino.utils.DateUtil;

@Controller
@RequestMapping("activeUser")
public class ActiveUserController {

	@Autowired
	private ActiveUserService activeUserService;

	@RequestMapping("activeUserAnalysis")
	public String visitorsAnalysis(ModelMap model) {
		return "application/activeUserAnalysis";
	}

	/**
	 * @Title: activeUserChartData
	 * @Description: 图表数据
	 * @param @param chooseTime
	 * @param @return 设定文件
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping("activeUserChartData")
	@ResponseBody
	public Map<String, Object> visitorChartData(String chooseTime) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		ActiveUser user = new ActiveUser();
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(chooseTime);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		user.setStartDate(beginDate);
		user.setEndDate(endDate);

		List<ActiveUser> activeUserListByDate = activeUserService
				.getUserListByDate(user);
		List<String> platformList = new ArrayList<String>();
		platformList.add("PC");
		platformList.add("IOS");
		platformList.add("Android");
		platformList.add("WAP");
		platformList.add("OTH");
		platformList.add("ALL");
		// 折线图数据
		String[] dateAxis = DateUtil.getBetweenTime(beginDate, endDate,
				"yyyy/MM/dd");
		List<String> dateList = Arrays.asList(dateAxis);

		JSONArray dauArr = new JSONArray();
		JSONArray mauArr = new JSONArray();
		for (String date : dateList) {
			for (String platform : platformList) {
				int dau = 0;
				int mau = 0;
				for (ActiveUser acUser : activeUserListByDate) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
					String time = sdf.format(acUser.getOperDate());
					if (acUser.getPlatform().equals(platform)
							&& time.equals(date)) {
						dau = acUser.getDau();
						mau = acUser.getMau();
					}
				}
				JSONObject o = new JSONObject();
				o.put("platform", platform);
				o.put("time", date);
				o.put("dau", dau);
				dauArr.add(o);

				JSONObject o2 = new JSONObject();
				o2.put("platform", platform);
				o2.put("time", date);
				o2.put("mau", mau);
				mauArr.add(o2);
			}
		}
		resultMap.put("platformList", platformList);
		resultMap.put("dauArrData", dauArr);
		resultMap.put("mauArrData", mauArr);
		return resultMap;
	}

	/**
	 * @Title: getQueryDate
	 * @Description: 查询条件起始时间的处理
	 * @param @param choose_date
	 * @param @return 设定文件
	 * @return Map<String,Date> 返回类型
	 * @throws
	 */
	private Map<String, Date> getQueryDate(String choose_date) {
		Map<String, Date> map = new HashMap<>();
		DateTimeFormatter formater = DateTimeFormat
				.forPattern("MM/dd/yyyy HH:mm:ss");
		Date beginDate = DateTime.parse(
				choose_date.split("-")[0].trim() + " 00:00:00", formater)
				.toDate();
		Date endDate = DateTime.parse(
				choose_date.split("-")[1].trim() + " 23:59:59", formater)
				.toDate();
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		return map;
	}

}
